// pages/smart/smart.js
const app=getApp();
const plugin=requirePlugin('WechatSI');
const manager=plugin.getRecordRecognitionManager();
let num=0;
// const db=wx.cloud.database(); 
Page({
    data: {
        scrollTop:"1300rpx",
        textInputTis:1, 
        tis:'长按发送语音',
        displayYuyin:"none",
        displayIpt:"block",
        rightTop:"0%",
        // 语音
        recordState:false,
        content:'',
        answer:'',
        src:'',
        is_clock:false,
        list:[{
            type:2,
            src:'',
            text:"有什么需要我帮助的吗？"
        }]
    },
    changeStyle:function(){
        num++;
        console.log(num);
        if(num%2==1){ 
            this.setData({
                displayYuyin:"block",
                displayIpt:"none"
            })
        }else{
            this.setData({
                displayYuyin:"none",
                displayIpt:"block"
            })
        }   
    },

    onLoad: function (options) { 
        // 识别语音
        this.initRecord();
    },
    //发送消息
    sendMsg:function(t){
        console.log(123)
        var that=this;
        let text=t.detail.value;
        if(text.container==''){
            wx.showToast({
                title:'请输入内容', 
                icon:'none',
            })
        }else{
            let arr={
                type:1,
                src:'',
                text:text.container
            };
            var a=that.data.list;
            console.log(a);
            a.push(arr);
            console.log(a)
            this.setData({
                content:text,
                list:a
            })   
            for(let i=0;i<a.length;i++){
                if(a[i].type==1){
                    wx.request({
                        url: 'https://wx.xxmtcds.top:2333/user/smart',
                        method:'POST',
                        header:{
                            authorization:app.globalData.token
                        },
                        data:{
                            content:a[i].text
                        }
                      })
                }
            }
            
            this.bottom()
            this.answer()
        }
        
    },
    bottom(){
        var that=this;
        that.setData({
            scrollTop:1000000
        })
    },
    //语音文字切换
    textInputTis(){
        if(this.data.textInputTis==1){
            this.setData({
                textInputTis:2
            })
        }else if(this.data.textInputTis==2){
            this.setData({
                textInputTis:1
            })
        }
    },
    answer:function(){
        console.log("adf")
        this.setData({
            answer:'123'
        })
        this.wordYun();
    },
    // 答案文字转语音
    wordYun:function(e){
        var that=this;
        let content=that.data.answer;
        console.log(content)
        plugin.textToSpeech({
            lang:"zh_CN",
            tts:true,
            content:content,
            success:function(res){
                console.log(res);
                console.log(res.filename)
                let arr={
                    type:2,
                    src:res.filename,
                    text:content
                };
                var a=that.data.list;
                a.push(arr)
                console.log(a)
                that.setData({
                    src:res.filename,
                    list:a
                })
                that.yuyinPlay();
                that.bottom()
            },
            fail:function(res){
                console.log(res)
            }
        })
    },
    // 播放语音
    yuyinPlay:function(e){
        if(this.data.src==""){
            console.log('暂无语音');
            return;
        }
        this.innerAudioContext.src=this.data.src;
        this.innerAudioContext.play();
    },
    preImage:function(e){
        console.log(e);
        let src=e.currentTarget.dataset.src;
        console.log(src)
        this.innerAudioContext.src=src
        this.innerAudioContext.play();
    },
    // 识别语音——初始化
    initRecord:function(){
        const that=this;
        manager.onRecognize=function(res){
            console.log(res);
        }
        manager.onStart=function(res){
            console.log("成功开始录音识别",res)
        }
        manager.onError=function(res){
            console.log("error msg",res);
        }
        manager.onStop=function(res){
            console.log(".....结束录音",res);
            console.log('录用临时文件地址：',res.tempFilePath);
            console.log('录音总时长：',res.duration+'ms');
            console.log('文件大小：',res.fileSize+'B');
            console.log('语音内容：',res.result);
            if(res.result==''){
                wx.showModal({
                    title:'提示',
                    content:'听不清楚，请重新说一遍',
                    showCancel:false,
                    success:function(res){}
                })
                return;
            }
            var text=res.result;
            console.log(text);
            let arr={
                type:1,
                src:res.tempFilePath,
                text:res.result
            };
            var a=that.data.list;
            a.push(arr)
            console.log(a);
            that.setData({
                content:text,
                list:a
            })
            that.answer();
            that.bottom();
        }
    },

    //语音  --按住说话
    touchStart:function(e){
        console.log('长按');
        this.setData({
            recordState:true,
            is_clock:false,
            startPoint:e.touches[0],
            tis:'录音中...'
        })
        manager.start({
            lang: 'zh_CN'
        })
        
    },
    // 语音 --松开结束
    touchEnd:function(e){
        console.log('结束');
        this.setData({
            tis:'长按发送语音'
        })
        manager.stop();
        
    },
    handleTouchMove:function(e){
        console.log('取消');
        if(Math.abs(e.touches[e.touches.length-1].clientY-this.data.startPoint.clientY)>25){
            this.setData({
                is_clock:false
            })
        }
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.innerAudioContext=wx.createInnerAudioContext();
        this.innerAudioContext.onError(function(res){
            console.log(res);
            wx.showToast({
                title:'语音播放失败',
                icon:'none'
            })
        })
    },

    onShow:function(){
        let style=wx.getStorageSync('style')
        if(style!=''){
            this.setData({
                contentStyle:wx.getStorageSync('style')
            })
        }else{
            this.setData({
                contentStyle:'https://pic.imgdb.cn/item/6199ad962ab3f51d91e21dd0.jpg'
            })
        }
        let children=wx.getStorageSync('children')
        if(children!==''){
            this.setData({
                children:wx.getStorageSync('children')
            })
        }else{
            this.setData({
                children:'https://pic.imgdb.cn/item/6199ab9b2ab3f51d91e171d6.png'
            })
        }
    },
    

})